package views
{
	import away3d.cameras.lenses.OrthographicLens;
	import away3d.cameras.lenses.PerspectiveLens;
	import away3d.containers.View3D;
	import away3d.debug.AwayStats;
	import away3d.lights.DirectionalLight;
	import away3d.lights.LightBase;
	import away3d.lights.LightProbe;
	import away3d.lights.PointLight;
	import away3d.lights.shadowmaps.NearDirectionalShadowMapper;
	import away3d.materials.methods.DitheredShadowMapMethod;
	import away3d.materials.methods.NearShadowMapMethod;
	import away3d.materials.methods.SoftShadowMapMethod;
	
	import flash.events.Event;
	import flash.geom.Vector3D;
	
	import tool.DataParser;
	
	/**
	 * 游戏中的3D视图
	 */	
	public class GameView extends View3D
	{
		private var lights:Array = new Array;
		
		public function GameView()
		{
			super(null, null, null, false);
			
			Global.gameView = this;
			camera.z = -100;
			antiAlias = 4;
			rightClickMenuEnabled = false;
			//backgroundColor = 0x87B0E7;
			
			var state:AwayStats = new AwayStats(this);
			state.x = 680;
			Global.stage.addChild(state);
			
			addEventListener(Event.ADDED_TO_STAGE,initView);
		}
		
		public function convertToScreenPos(pos:Vector3D):Vector3D
		{
			var vector3d:Vector3D=camera.inverseSceneTransform.transformVector(pos);
			
			var w:Number=width*.5;
			var h:Number=height *.5;
			
			vector3d.x=vector3d.x/vector3d.z*h+w;
			vector3d.y=(-vector3d.y/vector3d.z+1)*h;
			
			return new Vector3D(4*(vector3d.x-w)+w,4*(vector3d.y-h)+h,0);
		}
		
		public function getCamera(xml:XML):void
		{
			var type:String = xml.@type;
			var posStr:String = xml.@pos;
			var tarStr:String = xml.@tar;
			var fovStr:String = xml.@fov;
			
			var posAry:Array = posStr.split(",");
			var tarAry:Array = tarStr.split(",");
			
			var pos:Vector3D = new Vector3D(Number(posAry[0]),Number(posAry[1]),Number(posAry[2]));
			var tar:Vector3D = new Vector3D( Number(tarAry[0]), Number(tarAry[1]), Number(tarAry[2]));
			
			camera.position = pos;
			camera.lookAt(tar);
			
			var fov:Number = Number(fovStr)*Global.gameView.height/Global.gameView.width;
			
			if(type=="p")
			{
				camera.lens = new PerspectiveLens(fov);
				
			}else if(type=="o")
			{
				var sub:Vector3D = pos.subtract(tar);
				var dis:Number = sub.length;
				var pro:Number =dis * Math.atan(fov*Math.PI/180);
				
				camera.lens = new OrthographicLens(pro);
			}
			
			camera.lens.far = 30000;
		}
		
		public function getLight(xml:XML):void
		{
			var lightData:Object = DataParser.getLight(xml);
			
			lights = lightData.lights;
			for each(var l:LightBase in lights)scene.addChild(l);
			
			Global.shadowMethod = lightData.shadow;
			
			
			Global.lightPicker.lights = lights;
		}
		
		override public function dispose():void
		{
			stage.removeEventListener(Event.RESIZE,onResize);
			removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			super.dispose();
		}
		
		private function initView(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,initView);
			
			stage.addEventListener(Event.RESIZE,onResize);
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private function onResize(e:Event):void
		{
			width = stage.stageWidth;
			height = stage.stageHeight;
		}
		
		private function onEnterFrame(e:Event):void
		{
			render();
		}
	}
}